|
In computer programming, the scope of a name binding – an association of a name to an entity, such as a variable – is the part of a computer program where the binding is valid: where the name can be used to refer to the entity. In other parts of the program the name may refer to a different entity (it may have a different binding), or to nothing at all (it may be unbound). The scope of a binding is also known as the visibility of an entity, particularly in older or more technical literature – this is from the perspective of the referenced entity, not the referencing name. A scope is a part of a program that is or can be the scope for a set of bindings – a precise definition is tricky (see below), but in casual use and in practice largely corresponds to a block, a function, or a file, depending on language and type of entity. The term "scope" is also used to refer to the set of ''all'' entities that are visible or names that are valid within a portion of the program or at a given point in a program, which is more correctly referred to as ''context'' or ''environment''. Strictly speaking and in practice for most programming languages, "part of a program" refers to "portion of the source code (area of text)", and is known as lexical scope. In some languages, however, "part of a program" refers to "portion of run time (time period during execution)", and is known as dynamic scope. Both of these terms are somewhat misleading – they misuse technical terms, as discussed in the definition – but the distinction itself is accurate and precise, and these are the standard respective terms. Lexical scope is the main focus of this article, with dynamic scope understood by contrast with lexical scope. In most cases name resolution based on lexical scope is straightforward to use and to implement, as in use one can simply read backwards in the source code to determine to which entity a name refers, and in implementation one can simply maintain a list of names and contexts when compiling or interpreting a program. Basic difficulties arise in name masking, forward declarations, and hoisting, while considerably subtler ones arise with non-local variables, particularly in closures. ==Definition== The strict definition of the (lexical) "scope" of a name (identifier) is unambiguous – it is "the portion of source code in which a binding of a name with an entity applies" – and is virtually unchanged from its 1960 definition in the specification of ALGOL 60. Representative language specification follow. ALGOL 60 (1960):〔"Report on the Algorithmic Language Algol 60", 2.7. Quantities, kinds and scopes〕 :The following kinds of quantities are distinguished: simple variables, arrays, labels, switches, and procedures. :The scope of a quantity is the set of statements and expressions in which the declaration of the identifier associated with that quantity is valid. C (2007):〔(WG14 N1256 ) (2007 updated version of the C99 standard), 6.2.1 Scopes of identifiers, 2007-09-07〕 :An identifier can denote an object; a function; a tag or a member of a structure, union, or enumeration; a typedef name; a label name; a macro name; or a macro parameter. The same identifier can denote different entities at different points in the program. () :For each different entity that an identifier designates, the identifier is ''visible'' (i.e., can be used) only within a region of program text called its ''scope.'' Go (2013):〔(The Go Programming Language Specification ): (Declarations and scope ), Version of Nov 13, 2013〕 :A declaration binds a non-blank identifier to a constant, type, variable, function, label, or package. () :The scope of a declared identifier is the extent of source text in which the identifier denotes the specified constant, type, variable, function, label, or package. Most commonly "scope" refers to when a given name can refer to a given variable – when a declaration has effect – but can also apply to other entities, such as functions, types, classes, labels, constants, and enumerations. 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Scope (computer science)」の詳細全文を読む スポンサード リンク
|